home *** CD-ROM | disk | FTP | other *** search
/ Amiga Games Extra 1996 September / Amiga Games Extra CD-ROM 9-1996.iso / userbox / demoversionen / dcf77 demo v3 beta / für programmierer / waitdcf77ifcrash.c < prev    next >
C/C++ Source or Header  |  1996-01-10  |  3KB  |  113 lines

  1. /* WaitDCF77ifCrash                                                 ***RGR***
  2.  
  3.    Author:   Ralf Gruner, An der Sense 5a, D-02779 Großschönau, Germany.
  4. */
  5.  
  6. #include <exec/types.h>
  7. #include <exec/semaphores.h>
  8. #include <clib/exec_protos.h>
  9. #include <clib/dos_protos.h>
  10. #include <stdio.h>
  11.  
  12. BPTR    DateFile =NULL;
  13. char *filename1    ="ENVARC:LastBootDate";
  14. BPTR    CONWin =NULL;
  15. char *filename2    ="CON:20/20/280/80/WaitDCF77ifCrash/AUTO";
  16.  
  17. char *version="$VER: WaitDCF77ifCrash 1.03 (4.1.96)";
  18.  
  19. struct StatusList
  20. {
  21.     struct SignalSemaphore sl_Semaphore;
  22.     STRPTR version;
  23.     BOOL Received;
  24. };
  25.  
  26. struct StatusList *DCF77PublicStatus=NULL;
  27.  
  28. UBYTE    * DCF77SemaphoreName = "DCF77 State";
  29. BOOL    DCF77received=FALSE;
  30.  
  31. main(int argc, char *argv[])
  32. {
  33.     struct DateStamp DS, DSfromFile;
  34.     long bytes;
  35.     char *msg1="";
  36.     char *msg2=    " © 1994-1996 Ralf Gruner, Großschönau, Germany.\n\n"
  37.                     " WaitDCF77ifCrash waits for received time from DCF77\n if the date of your hardware clock has been corrupted.\n"
  38.                     " Usage: Place it in a script above your date sensitive application.\n\n";
  39.  
  40.     if(argc != 1)
  41.     {
  42.         puts(msg1);
  43.         puts(version+5);
  44.         puts(msg2);
  45.     }
  46.     else
  47.     {
  48.         DateStamp((struct DateStamp *)&DS);    // Systemzeit in Struktur eintragen
  49.         
  50.         if (NULL!=(DateFile = Open(filename1,MODE_OLDFILE)))
  51.         {
  52.             bytes = Read(DateFile,&DSfromFile,(LONG)sizeof(struct DateStamp));
  53.             Close(DateFile);
  54.     
  55.             if (bytes != (LONG)sizeof(struct DateStamp))
  56.             {
  57.                 DSfromFile.ds_Days=0;
  58.             }
  59.     
  60.         }
  61.         else
  62.             DSfromFile.ds_Days=0;
  63.  
  64.         if(DSfromFile.ds_Days > DS.ds_Days || DSfromFile.ds_Days < DS.ds_Days-182)
  65.         {
  66.             Delay(500L);    // ggf. auf Workbench-Window warten
  67.             if (NULL!=(CONWin = Open(filename2,MODE_NEWFILE)))
  68.                 FPrintf(CONWin,"\n\n Waiting for DCF77...\n\n");
  69.  
  70.             while(!DCF77received)
  71.             {
  72.                 Forbid();
  73.                 if(DCF77PublicStatus=(struct StatusList *) FindSemaphore(DCF77SemaphoreName))
  74.                 {
  75.                     ObtainSemaphoreShared((struct SignalSemaphore *) DCF77PublicStatus);
  76.                     DCF77received=DCF77PublicStatus->Received;
  77.                     ReleaseSemaphore((struct SignalSemaphore *) DCF77PublicStatus);
  78.                 }
  79.                 else
  80.                     DCF77received=FALSE;    // DCF77 läuft nicht
  81.                 Permit();
  82.     
  83.                 if(!DCF77received)
  84.                     Delay(100L);    // zwei Sekunden warten und dann erneut testen
  85.             }
  86.  
  87.             DateStamp((struct DateStamp *)&DS);    // neue Systemzeit in Struktur eintragen
  88.  
  89.             if(CONWin)
  90.             {
  91.                 FPrintf(CONWin," O.K.\n");
  92.                 Delay(150L);
  93.                 Close(CONWin);
  94.             }
  95.         }
  96.  
  97.         if(DSfromFile.ds_Days != DS.ds_Days)
  98.         {
  99.             // nur speichern, wenn Tag geändert
  100.             if (NULL!=(DateFile = Open(filename1,MODE_NEWFILE)))
  101.             {
  102.                 bytes = Write(DateFile,&DS,(LONG)sizeof(struct DateStamp));
  103.                 if (bytes != (LONG)sizeof(struct DateStamp))
  104.                 {
  105.                     puts("Error while writing");
  106.                 }
  107.                 Close(DateFile);
  108.             }
  109.         }
  110.     }
  111. }
  112.  
  113.